package org.codeanalyzer;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;

@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("*")
public class ApiAnalyzerProcessor extends AbstractProcessor
{
    private Trees trees;
    private final TreePathScanner<Object, Trees> visitor;

    public ApiAnalyzerProcessor(TreePathScanner<Object, Trees> visitor)
    {
        this.visitor = visitor;
    }

    @Override
    public void init(ProcessingEnvironment pe)
    {
        super.init(pe);
        trees = Trees.instance(pe);
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment)
    {

        for(Element e : roundEnvironment.getRootElements())
        {
            TreePath tp = trees.getPath(e);
            if (tp != null)
            {
                visitor.scan(tp, trees);
            }
        }

        return true;
    }

}
